// 导入db模块
const db = require('../config/db');
// 数据表：db_vehicle
// 车辆信息管理类
class Vehicle {
    // 添加车辆(车牌号码，车颜色，车主，联系电话，车型)
    // 外键：user_id
    add(user_id,vehicle_num, vehicle_color, vehicle_owner, vehicle_phone, vehicle_type) {
        return new Promise((resolve, reject) => {
            // 查询sql语句
            let sql = `select vehicle_num from db_vehicle where vehicle_num='${vehicle_num}' and user_id='${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        // 执行成功
                        if (rows.length > 0) {
                            // 表示账号已经存在
                            resolve({
                                code: 200,
                                msg: '该车辆信息已登记。',
                                user_phone
                            })
                        }
                        else {
                            // 添加账号信息
                            // 插入数据sql语句
                            let sql2 = `insert into db_vehicle(user_id,vehicle_num, vehicle_color, vehicle_owner, vehicle_phone,vehicle_type) values('${user_id}','${vehicle_num}','${vehicle_color}','${vehicle_owner}', '${vehicle_phone}','${vehicle_type}')`;
                            // 返回添加数据的promise
                            return db.query(sql2)

                        }
                    },
                    () => {
                        // 执行失败
                        reject({
                            code: -1,
                            msg: '添加车辆信息失败。'
                        })
                    }
                )
                .then(
                    (data) => {
                        resolve({
                            code: 200,
                            msg: '添加车辆信息成功。',
                            vehicle_num,
                            vehicle_color
                        })
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '添加车辆信息失败。',
                            err
                        })
                    }
                )
        })
    }
    // 获取车辆信息
    info(user_id) {
        return new Promise((resolve, reject) => {
            // 查询账号
            let sql = `select user_id,vehicle_num, vehicle_color, vehicle_owner, vehicle_phone, vehicle_type from db_vehicle where user_id = '${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        // console.log('y.2',rows);
                        if (rows) {
                            resolve({
                                code: 200,
                                msg: '获取车辆信息成功。',
                                data: rows
                            })
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '尚未添加车辆。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询车辆失败。'
                        })
                    }
                )
        })
    }
    // 删除车辆信息
    remove(vehicle_num) {
        return new Promise((resolve, reject) => {
            // 查询账号
            let sql = `select vehicle_num from db_vehicle where vehicle_num = '${vehicle_num}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                            // 删除数据表的sql语句
                            let sql2 = `delete from db_vehicle where vehicle_num = '${vehicle_num}'`
                            db.query(sql2).then(
                                (result) => {
                                    if (result.affectedRows === 1) {
                                        resolve({
                                            code: 200,
                                            msg: '删除车辆信息成功。',
                                        })
                                    }
                                    else {
                                        resolve({
                                            code: -1,
                                            msg: '删除车辆信息失败。',
                                        })
                                    }
                                },
                                err => {
                                    reject(err)
                                }
                            )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该车辆信息不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询车辆信息失败。'
                        })
                    }
                )
        })
    }
    // 获取该用户所有车辆总数
    total(user_id) {
        return new Promise((resolve, reject) => {
            let sql = `select count(*) as total from db_vehicle where user_id = '${user_id}'`
            db.query(sql)
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: `统计车辆总数成功。`,
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '统计车辆总数失败。'
                        })
                    }
                )
        })
    }
    // 所有车辆信息列表记录
    list(page, pageSize) {
        return new Promise((resolve, reject) => {
            //获取分页数据
            let page_num = ((parseInt(page) - 1) * parseInt(pageSize))
            let page_size = (parseInt(pageSize))
            let sql = `select * from db_vehicle limit ${page_num} , ${page_size}`;
            let sql2 = `select count(*) as total from db_vehicle`

            db.query(sql)
                .then(
                    (rows) => {
                        let data;
                        db.query(sql2)
                            .then(
                                (obj) => {
                                    data = obj
                                    resolve({
                                        code: 200,
                                        msg: '获取所有车辆信息成功。',
                                        result: rows,
                                        page,
                                        pageSize,
                                        data
                                    })
                                }
                            )
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '获取所有车辆信息失败。',
                            err
                        })
                    }
                )
        })
    }
}
// 暴露车辆信息管理类实例
module.exports = new Vehicle();